home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15309 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: trib.apple.com!usenet
  2. From: Amir <Amir@bayarea.net>
  3. Newsgroups: comp.sys.lang.c++,comp.lang.c++
  4. Subject: Re: Help needed in C++ Function templates
  5. Date: Thu, 04 Apr 1996 11:23:13 -0700
  6. Organization: Apple Computer, Inc., Cupertino, California
  7. Message-ID: <31641391.2FCA@bayarea.net>
  8. References: <NEWTNews.828668110.2725.seedy@trigent.trigent.com>
  9. NNTP-Posting-Host: 17.128.203.75
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (Macintosh; I; PPC)
  14.  
  15. seedy@ultranet.com wrote:
  16. > When attempted to compile the following code using GNU's g++ (ver. 2.7.1)
  17. > on HP-UX, I got the following error :
  18. > news.cc: In function `int equal(const class abc &, const class abc &)':
  19. > news.cc:23: no match for `operator ==(class abc, class abc)'
  20. > #include <iostream.h>
  21. > class abc {
  22. >     public :
  23. >        abc( int x )
  24. >           {
  25. >           a = x;
  26. >           b = 0;
  27. >           }
  28. >        int a;
  29. >        int b;
  30. >        inline int operator ==(abc x)
  31. >            {
  32. >               return ((a==x.a) && (b==x.b));
  33. >            }
  34. >     };
  35. > template < class Element>
  36. > inline int equal (Element const& e1, Element const& e2)
  37. > {
  38. >   return e1 == e2;
  39. > }
  40. > main()
  41. > {
  42. > abc a1(5), a2(5);
  43. > cout << "Return value is = " << equal(a1, a2) << endl;
  44. > }
  45. > Even though the 'operator ==' is overloaded in class abc, compiler is
  46. > reporting match not found. By making the overloaded function 'operator =='
  47. > as friend to 'class abc' I could resolve the problem, because in this case,
  48. > this overloaded function becomes outsider function.
  49. > As far as I know, template instantiation for function 'equal will be done
  50. > for 'class abc'(because the passed parameters are of type class abc),
  51. > and so the corresponding member function 'operator ==' can be used for
  52. > template variables e1 and e2.
  53. > Could any one explain why this error happens.  Please send
  54. > your response via mail to 'seedy@trigent.com' or post it here.
  55. > Cheers,
  56. > Praveen.
  57.  
  58. You are calling with const parameters a method that
  59. it's parameters are not const.
  60. change your operator== to:
  61.  
  62.        inline int operator ==(const abc & x) const
  63.            {
  64.               return ((a==x.a) && (b==x.b));
  65.            }
  66.  
  67. Amir
  68.  
  69.  
  70. --
  71. --        o o
  72. -----ooo---O---ooo-----------------------------------------
  73. --  Amir Deutel
  74. --
  75. --  Home: Amir@bayarea.net    http://www.bayarea.net/~amir/
  76. --  Work: Amir_Deutel@powertalk.apple.com
  77.